﻿using System;
using UILib;
using UnityEngine;
using XMainClient.UI;
using XMainClient.UI.UICommon;
using XUtliPoolLib;

namespace XMainClient
{
	// Token: 0x02000FBE RID: 4030
	internal class XBubbleComponent : XComponent
	{
		// Token: 0x170036AA RID: 13994
		// (get) Token: 0x0600D178 RID: 53624 RVA: 0x0030A2B4 File Offset: 0x003084B4
		public override uint ID
		{
			get
			{
				return XBubbleComponent.uuID;
			}
		}

		// Token: 0x0600D179 RID: 53625 RVA: 0x0030A2CB File Offset: 0x003084CB
		public XBubbleComponent()
		{
			this._hideBubbleCb = new XTimerMgr.ElapsedEventHandler(this.HideBubble);
		}

		// Token: 0x0600D17A RID: 53626 RVA: 0x0030A308 File Offset: 0x00308508
		public override void OnAttachToHost(XObject host)
		{
			base.OnAttachToHost(host);
			this._billboard = XSingleton<XResourceLoaderMgr>.singleton.CreateFromPrefab(XBubbleComponent.BUBBLE_TEMPLATE, this._entity.EngineObject.Position, this._entity.EngineObject.Rotation, true, false);
			this._billboard.name = this._entity.ID.ToString();
			bool isNpc = this._entity.IsNpc;
			if (isNpc)
			{
				XSingleton<UiUtility>.singleton.AddChild(XSingleton<XGameUI>.singleton.NpcHpbarRoot.gameObject, this._billboard);
			}
			else
			{
				XSingleton<UiUtility>.singleton.AddChild(XSingleton<XGameUI>.singleton.HpbarRoot.gameObject, this._billboard);
			}
			float val = 0.9f;
			this._heroHeight = Math.Max(this._entity.Height, val);
			this._billboard.transform.localScale = new Vector3(0.01f * this._heroHeight / 1.5f, 0.01f * this._heroHeight / 1.5f, 0.01f * this._heroHeight / 1.5f);
			this._text = (this._billboard.transform.Find("chattext/text/content").GetComponent("XUILabelSymbol") as IXUILabelSymbol);
			this._label = (this._billboard.transform.Find("chattext/text/content").GetComponent("XUILabel") as IXUILabel);
			this._board = (this._billboard.transform.Find("chattext/text/content/board").GetComponent("XUISprite") as IXUISprite);
			this.mCameraTrans = XSingleton<XScene>.singleton.GameCamera.CameraTrans;
			this.mSrcDistance = 8f;
		}

		// Token: 0x0600D17B RID: 53627 RVA: 0x0030A4D0 File Offset: 0x003086D0
		protected override void EventSubscribe()
		{
			base.RegisterEvent(XEventDefine.XEvent_Bubble, new XComponent.XEventHandler(this.ShowBubble));
		}

		// Token: 0x0600D17C RID: 53628 RVA: 0x0030A4E8 File Offset: 0x003086E8
		public override void OnDetachFromHost()
		{
			bool flag = this._timer > 0U;
			if (flag)
			{
				XSingleton<XTimerMgr>.singleton.KillTimer(this._timer);
			}
			this.DestroyGameObjects();
			base.OnDetachFromHost();
		}

		// Token: 0x0600D17D RID: 53629 RVA: 0x0030A522 File Offset: 0x00308722
		protected void DestroyGameObjects()
		{
			XResourceLoaderMgr.SafeDestroy(ref this._billboard, true);
		}

		// Token: 0x0600D17E RID: 53630 RVA: 0x0030A534 File Offset: 0x00308734
		protected bool ShowBubble(XEventArgs e)
		{
			XBubbleEventArgs xbubbleEventArgs = e as XBubbleEventArgs;
			this._billboard.gameObject.SetActive(true);
			this._text.InputText = xbubbleEventArgs.bubbletext;
			DlgBase<XChatSmallView, XChatSmallBehaviour>.singleton.ShowCurrTempMsg(xbubbleEventArgs.bubbletext, xbubbleEventArgs.speaker);
			bool flag = this._timer > 0U;
			if (flag)
			{
				XSingleton<XTimerMgr>.singleton.KillTimer(this._timer);
			}
			this._timer = XSingleton<XTimerMgr>.singleton.SetTimer(xbubbleEventArgs.existtime, this._hideBubbleCb, null);
			return true;
		}

		// Token: 0x0600D17F RID: 53631 RVA: 0x0030A5C4 File Offset: 0x003087C4
		protected void HideBubble(object o)
		{
			this._billboard.gameObject.SetActive(false);
			this._timer = 0U;
		}

		// Token: 0x0600D180 RID: 53632 RVA: 0x0030A5E0 File Offset: 0x003087E0
		public override void PostUpdate(float fDeltaT)
		{
			bool flag = this._billboard == null;
			if (!flag)
			{
				XEntity xentity = this._host as XEntity;
				bool flag2 = xentity == null;
				if (!flag2)
				{
					bool flag3 = !XEntity.ValideEntity(xentity) && !xentity.IsAffiliate;
					if (flag3)
					{
						this._billboard.SetActive(false);
					}
					else
					{
						float num = 0.2f + this._entity.Height;
						bool isAffiliate = this._entity.IsAffiliate;
						if (isAffiliate)
						{
							num = 0.05f + this._entity.Height / 2f;
						}
						this._entity.EngineObject.SyncPos();
						Vector3 position = new Vector3(this._entity.EngineObject.Position.x, this._entity.EngineObject.Position.y + num, this._entity.EngineObject.Position.z);
						this._billboard.transform.position = position;
						this._billboard.transform.rotation = XSingleton<XScene>.singleton.GameCamera.Rotaton;
						bool flag4 = this.mCameraTrans != null;
						if (flag4)
						{
							float num2 = Vector3.Distance(this._entity.EngineObject.Position, this.mCameraTrans.position);
							float num3 = num2 / this.mSrcDistance;
							this._heroHeight = (this._entity.IsAffiliate ? 1.6f : 2f) * Mathf.Clamp(num3, 0.2f, 1.2f);
							this._billboard.transform.localScale = new Vector3(0.01f * this._heroHeight / 1.5f, 0.01f * this._heroHeight / 1.5f, 0.01f * this._heroHeight / 1.5f);
						}
						bool flag5 = Time.frameCount % 15 == 0;
						if (flag5)
						{
							float billBoardDepth = Vector3.Distance(XSingleton<XScene>.singleton.GameCamera.UnityCamera.transform.position, this._billboard.transform.position);
							this.SetBillBoardDepth(billBoardDepth);
						}
					}
				}
			}
		}

		// Token: 0x0600D181 RID: 53633 RVA: 0x0030A820 File Offset: 0x00308A20
		private void SetBillBoardDepth(float dis = 0f)
		{
			int num = -(int)(dis * 100f);
			bool flag = this._label != null && this._board != null;
			if (flag)
			{
				this._label.spriteDepth = num + 1;
				this._board.spriteDepth = num;
			}
		}

		// Token: 0x04005EFB RID: 24315
		private GameObject _billboard = null;

		// Token: 0x04005EFC RID: 24316
		private float _heroHeight = 0f;

		// Token: 0x04005EFD RID: 24317
		public static string BUBBLE_TEMPLATE = "UI/Billboard/Bubble";

		// Token: 0x04005EFE RID: 24318
		public new static readonly uint uuID = XSingleton<XCommon>.singleton.XHash("Bubble");

		// Token: 0x04005EFF RID: 24319
		private IXUILabelSymbol _text;

		// Token: 0x04005F00 RID: 24320
		private IXUILabel _label;

		// Token: 0x04005F01 RID: 24321
		private IXUISprite _board;

		// Token: 0x04005F02 RID: 24322
		private uint _timer = 0U;

		// Token: 0x04005F03 RID: 24323
		private XTimerMgr.ElapsedEventHandler _hideBubbleCb = null;

		// Token: 0x04005F04 RID: 24324
		private Transform mCameraTrans;

		// Token: 0x04005F05 RID: 24325
		private float mSrcDistance;
	}
}
